﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace FacialClub.Helpers
{
    /// <summary>
    /// Login Link Helper
    /// </summary>
    public class LoginLinkHelpers
    {
        /// <summary>
        /// MVCHtmlString
        /// </summary>
        /// <param name="helper"></param>
        /// <returns></returns>
        public static MvcHtmlString LoginLink(HtmlHelper helper)
        {
            var currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
            var currentActionName = (string)helper.ViewContext.RouteData.Values["action"];
            bool isAuthenticated = helper.ViewContext.HttpContext.Request.IsAuthenticated;
            var sb = new StringBuilder();
            if (isAuthenticated)
            {
                sb.Append("Logged in as <span>");
                sb.Append(helper.Encode(helper.ViewContext.HttpContext.User.Identity.Name)); 
                sb.Append("</span>");
            } 
            sb.Append("<div id=\"loginlink\""); 
            if (currentControllerName.Equals("Account", StringComparison.CurrentCultureIgnoreCase) && (currentActionName.Equals("Login", StringComparison.CurrentCultureIgnoreCase) || currentActionName.Equals("Logout", StringComparison.CurrentCultureIgnoreCase)))           
                sb.Append(" class=\"selected\">"); 
            else           
                sb.Append(">");
            sb.Append(isAuthenticated
                          ? helper.ActionLink("Logout", "Logout", "Account")
                          : helper.ActionLink("Login", "Logon", "Account"));
            sb.Append("</div>"); 
            return MvcHtmlString.Create(sb.ToString());
        } 
    }
}